# -*- coding: utf-8 -*-
"""
Created on Fri May 31 13:17:23 2013

@author: Maxim
"""
from time import gmtime, strftime,localtime

class Report:
    def __init__(self):
        self.out = ''
        self._line1 = '{0:18} {1:8.2f} {2:10.4f} {3:10.4f} {4:10.4f}\n'
    def new_line(self,n=1):
        self.out += '\n'*n
    def add_title(self,title,underline='='):
        self.out += title
        self.out += '\n' + underline*len(title)
    def add_tabulated_line(self,name,dataArray):
        pass
    def add_mass_header(self):
        lineFormat = '{0:20}|{1:8}|{2:10}|{3:10}|{4:10}|\n'
        out = lineFormat.format('Name','Mass,kg','CG:X,m','CG:Y,m','CG:Z,m')
        self.out += '='*len(out) + '\n' + out + '-'*len(out) + '\n'
    def add_mass_line1(self,name,mass,CG,header=False):
        out  = '========== Mass and Balance status ==========\n'
        out += '{0:16} | {1:20}\n'.format('loading case',name)
        out += '{0:10}({1:^4}) | {2:<8.2f}\n'.format('mass','kg',mass)
        out += '{0:10}({1:^4}) | [{2:<+8.4f}{3:^+8.4f}{4:>+8.4f}]\n'.format('CG','m',CG[0],CG[1],CG[2])
        out += '-'*45+'\n'
        self.out += out
    def add_mass_line(self,name,mass,CG,header=False):
        if header:
            self.add_mass_header()
        out = self._line1.format(name,mass,CG[0],CG[1],CG[2])
        self.out += out + '='*len(out) + '\n'
    def add_stability_line(self,name,values,zerosTE=4):
        lineFmt = '{0:+10.' + str(zerosTE) + 'f}'
        out = '{0:25}|'.format(name)
        for val in values:
            out += lineFmt.format(val)
        self.out += out + '\n'


def header(title,underline='-'):
    limiter = underline*len(title) + '\n'
    return title + '\n' + limiter

def header_bounded(title,underline='-'):
    limiter = underline*len(title) + '\n'
    return limiter + title + '\n' + limiter

def header_inline(title,underline='-',lineLen=50):
    n = int((lineLen-len(title)-2)/2)
    limiter = n*underline
    return limiter + ' ' + title + ' ' + limiter + '\n'

def header_inline1(title,underline='-',marker='>',lineLen=4):
    limiter = underline*(lineLen-1) + marker + ' '
    return limiter + title + '\n'
    

def line(name,unit=None,nUnit=10):
    out = '{0:35}'.format(name)
    if not unit==None:
        n = nUnit-2-len(unit)
        out += n*' '+'[%s]'%unit
    else:
        out += ' '*nUnit
    return out + ' :'
    
def value(data,nzeros=2):
    fmt = '{0:+8.%df}'%nzeros
    out = ''
    if hasattr(data,'__iter__'):
        for d in data:
            out += fmt.format(d)
    else:
        out = fmt.format(data)
    return out + '\n'

def show_date():
    return strftime("%d %b %Y %H:%M:%S", localtime()) + '\n'

class StabilityTextReport:
    def __init__(self,result,velocity,mass,CG,inertia=None):
        self.rslt     = result
        self.velocity = velocity
        self.mass     = mass
        self.CG       = CG
        self.inertia  = inertia
    def _report(self,showHeader=True,static=True,dynamic=True):
        out =''
        if showHeader:
            out += header('Stability Report','=')
            out += show_date()
        out += header_inline1(self.rslt.name,'=')
        out += header_inline('Input data')
        out += line('Mass','kg') + value(self.mass)
        out += line('Center of Gravity','m') + value(self.CG,4)
        if not self.inertia==None:
            out += line('Moment of inertia','kg*m2') + value(self.inertia)
        out += line('Velocity','m/s') + value(self.velocity)
        if static:
            out += header_inline('Static stability')
            out += line('Neutral point','m') + value(self.rslt.xNP,4)
            out += line('Static margin','%') + value(self.rslt.SM*100.0)
            out += line('Elevator deflection','deg') + value(self.rslt.elevator)
            out += line('Sideslip yaw moment coefficient') + value(self.rslt.derivs.Cnb)
            out += line('Sideslip roll moment coefficient') + value(self.rslt.derivs.Clb)
        if dynamic:
            out += header_inline('Dynamic stability')
            out += header_inline1('Longitudinal modes')
            out += line('Short period natural frequency','rad/s') 
            out += value(self.rslt.dynamicStability.shortPeriod.Wn)
            out += line('Short period damping ratio') 
            out += value(self.rslt.dynamicStability.shortPeriod.Z)
            out += line('Phugoid natural frequency','rad/s')
            out += value(self.rslt.dynamicStability.phugoid.Wn)
            out += line('Phugoid damping ratio')
            out += value(self.rslt.dynamicStability.phugoid.Z)
            out += header_inline1('Lateral modes')
            out += line('Dutch roll natural frequency','rad/s')
            out += value(self.rslt.dynamicStability.dutchRoll.Wn)
            out += line('Dutch roll damping ratio')
            out += value(self.rslt.dynamicStability.dutchRoll.Z)
            out += line('Spiral time to double/half','s')
            out += value(self.rslt.dynamicStability.spiral.T2)
            out += line('Roll time to double/half','s')
            out += value(self.rslt.dynamicStability.roll.T2)
        return out
        
    def write_txt(self,path):
        pass
    def display(self):
        pass

def run_test():
    sr = StabilityTextReport(1,10.0,620.0,[200.0,100.,50])
    sr._report()

if __name__=="__main__":
    run_test()